package j_util.graphics.gui;

import j_util.graphics.auxiliary.GraphicsUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class GraphicDialogAutSave extends JDialog {
    private DrawPlotPanel drawPlotPanel;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton showFrecuencyButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton scaleButton;
    private javax.swing.JSeparator separator;
    private javax.swing.JSeparator separatorBottom;
    private javax.swing.JLabel titleLabel;

    private String unit;
    private String imageName;
    private int imageNumber;
    private double scaleValue;
    private String path;
    private boolean canceled;
    private int frequency;

    public GraphicDialogAutSave(String imageName) {
        canceled = false;
        scaleValue = 1;
        this.imageName = imageName;
        unit = "";
        initComponents(null, null);
    }

    public GraphicDialogAutSave(double[] x, double[] y, double scale, String scaleUnit, String imageName) {
        canceled = false;
        scaleValue = scale;
        unit = scaleUnit;
        this.imageName = imageName;
        initComponents(x, y);
    }

    private void initComponents(double[] x, double[] y) {
        path = "/LCDTerminal/Graphics";
        drawPlotPanel = new DrawPlotPanel(x, y, scaleValue, unit);
        titleLabel = new javax.swing.JLabel();
        separator = new javax.swing.JSeparator();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        showFrecuencyButton = new javax.swing.JButton();
        scaleButton = new javax.swing.JButton();
        separatorBottom = new javax.swing.JSeparator();
        drawPlotPanel.setSize(700, 680);


        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        titleLabel.setText("Graphic");

        saveButton.setText("Save graphic");
        saveButton.setMnemonic(KeyEvent.VK_ENTER);
        saveButton.setToolTipText("Press ALT+ENTER");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    File file = new File(GraphicsUtil.getConfigFileData(path) + "/" + imageName + "_" + imageNumber + ".png");
                    ImageIO.write(drawPlotPanel.getBufferedImage(), "png", file);
                    imageNumber++;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.setMnemonic('c');
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GraphicsUtil.updateConfigFile(path);
                canceled = true;
                dispose();
            }
        });


        showFrecuencyButton.setText("Show frequency");
        showFrecuencyButton.setMnemonic('f');
        showFrecuencyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(GraphicDialogAutSave.this, "Frequency: " + frequency, "", JOptionPane.INFORMATION_MESSAGE);//todo getFrecuency
            }
        });

        scaleButton.setText("Scale");
        scaleButton.setMnemonic('s');
        scaleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ScaleDialog scaleDialog = new ScaleDialog();
                scaleDialog.setLocation(scaleButton.getX(), scaleButton.getY() - 50);
                scaleDialog.setModal(true);
                scaleDialog.setVisible(true);
                scaleValue = scaleDialog.getScaleValue();
                updateGraphic(null, null, scaleValue, unit);
            }
        });
        scaleButton.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(titleLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(separator, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE)
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(separatorBottom, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton)
                                .addGap(2)
                                .addComponent(showFrecuencyButton)
                                .addGap(2)
                                .addComponent(scaleButton)
                                .addGap(2)
                                .addComponent(saveButton)
                        )
                        .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(drawPlotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(titleLabel)
                                .addGroup(layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(drawPlotPanel, 660, 660, 660)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(cancelButton)
                        .addComponent(showFrecuencyButton)
                        .addComponent(scaleButton)
                        .addComponent(saveButton)
                        .addComponent(separatorBottom, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        pack();
    }

    public void updateGraphic(double[] x, double[] y, double scale, String legend) {
        unit = legend;
        drawPlotPanel.createGraphic(x, y, scale, unit);
    }

//    public void setGraphicSize(int width, int height){
//        drawPlotPanel.setSize(width,height);
//    }

    public boolean isCanceled() {
        return canceled;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    public static void main(String[] args) {
        double[] y = new double[]{65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        GraphicDialogAutSave graphicDialogAutSave = new GraphicDialogAutSave("Prueba");

        graphicDialogAutSave.setVisible(true);
        graphicDialogAutSave.updateGraphic(null, y, 1, "10e-6Hz");
    }
}
